home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / ColorSync 2.5.1 SDK / Sample Code / CSDemo 2.5 / ShellSources / resourceUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-09  |  3.7 KB  |  132 lines  |  [TEXT/CWIE]

  1. // This file contains useful routines for loading
  2. // common resource data types.
  3. // 
  4. // 9/16/94    david    first cut
  5. // 9/20/95    david    improved comments
  6.  
  7.  
  8. #include <Errors.h>
  9. #include <Resources.h>
  10. #include <TextUtils.h>
  11. #include "resourceUtils.h"
  12.  
  13.  
  14. /**\
  15. |**| ==============================================================================
  16. |**| PRIVATE TYPEDEFS
  17. |**| ==============================================================================
  18. \**/
  19. typedef struct OSTypeList
  20. {
  21.     short    count ;            // 1-based count of structs to follow 
  22.     struct
  23.     {
  24.         OSType    type ;
  25.         Str27    string ;
  26.     } entry[];
  27. } OSTypeList, *OSTypeListPtr, **OSTypeListHdl ;
  28.  
  29.  
  30.  
  31. /**\
  32. |**| ==============================================================================
  33. |**| PUBLIC FUNCTIONS
  34. |**| ==============================================================================
  35. \**/
  36.  
  37.  
  38. /*------------------------------------------------------------------------------*\
  39.     GetRect
  40.  *------------------------------------------------------------------------------*
  41.         This routine loads a 'RECT' resource.
  42.         If the resource id can't be found then it return the rest {0,0,0,0}.
  43. \*------------------------------------------------------------------------------*/
  44. Rect GetRect ( short id )
  45. {
  46.     Handle        rectHndl ;
  47.     Rect        rect = {0,0,0,0} ;
  48.     
  49.     rectHndl = GetResource( 'RECT', id ) ;
  50.     if (rectHndl != nil )
  51.     {
  52.         rect = **((Rect**)(rectHndl)) ;
  53.         ReleaseResource( rectHndl ) ;
  54.     }
  55.     return rect ;
  56. }
  57.  
  58.  
  59. /*------------------------------------------------------------------------------*\
  60.     GetStringPtr
  61.  *------------------------------------------------------------------------------*
  62.         This routine loads a 'STR ' resource.
  63.         It is similar to the GetString routine except that it returns a StringPtr
  64.         instead of a StringHandle.
  65. \*------------------------------------------------------------------------------*/
  66. OSErr GetStringPtr ( short id, StringPtr dest )
  67. {
  68.     StringHandle    strHndl ;
  69.     
  70.     strHndl = GetString( id ) ;
  71.     if (!strHndl) return ResError() ;
  72.  
  73.     HLock( (Handle)strHndl ) ;
  74.     BlockMove( *strHndl, dest, (*strHndl)[0]+1) ;
  75.     ReleaseResource( (Handle)strHndl ) ;
  76.     return noErr ;
  77. }
  78.  
  79.  
  80. /*------------------------------------------------------------------------------*\
  81.     GetErrStringPtr
  82.  *------------------------------------------------------------------------------*
  83.         This routine loads a 'Estr' resource.
  84.         It is similar to the GetString routine except that it returns a StringPtr
  85.         instead of a StringHandle.
  86. \*------------------------------------------------------------------------------*/
  87. OSErr GetErrStringPtr ( short id, StringPtr dest )
  88. {
  89.     Handle    strHndl ;
  90.     
  91.     strHndl = GetResource( 'Estr', id ) ;
  92.     if (!strHndl) return ResError() ;
  93.     
  94.     HLock( strHndl ) ;
  95.     BlockMove( *strHndl, dest, (*strHndl)[0]+1) ;
  96.     ReleaseResource( strHndl ) ;
  97.     return noErr ;
  98. }
  99.  
  100.  
  101. /*------------------------------------------------------------------------------*\
  102.     GetOSTypeListStringPtr
  103.  *------------------------------------------------------------------------------*
  104.         This routine loads a 'OST#' resource and loops through it looking
  105.         for the string that coresponds to the type parameter.
  106. \*------------------------------------------------------------------------------*/
  107. OSErr GetOSTypeListStringPtr ( short id, OSType type, StringPtr dest )
  108. {
  109.     short            i, count ;
  110.     OSTypeListHdl    list ;
  111.     StringPtr        src    ;
  112.     
  113.     list = (OSTypeListHdl) GetResource('OST#', id) ;
  114.     if (list==nil) return ResError() ;
  115.     
  116.     // try to find the type in the list
  117.     count = (**list).count ;
  118.     for (i=0; i<count; i++ )
  119.         if ((**list).entry[i].type == type )
  120.         {
  121.             src = (StringPtr)&((**list).entry[i].string);
  122.             HLock( (Handle)list ) ;
  123.             BlockMove(src, dest, src[0]+1) ;
  124.             ReleaseResource( (Handle)list ) ;
  125.             return noErr ;
  126.         }
  127.         
  128.     // we didnt find a match
  129.     ReleaseResource( (Handle)list ) ;
  130.     return paramErr ;
  131. }
  132.